package cool.tianyou.blue.bird.util;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.preference.PreferenceManager;

import cool.tianyou.blue.bird.model.Log;
import cool.tianyou.blue.bird.model.Message;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class LogUtil {
    public static List<Listener> listeners = new ArrayList<>();
    public static LinkedList<Log> logs = new LinkedList<>();

    public static void registerListener(Listener listener) {
        listeners.add(listener);
    }

    public static String lastLog = "";

    public static synchronized void recordLog(Log log, Context context) {
        lastLog = log.toString();
        if (log.getSaveToLogList()) {
            logs.addFirst(log);
        }
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
        int saveLogSize = settings.getInt("save_log_size", 10);
        if (logs.size() > saveLogSize) {
            logs.removeLast();
        }

        if (listeners != null) {
            for (Listener listener : listeners) {
                listener.call(1);
            }
        }

    }

    public static void updateLog() {
        if (listeners != null) {
            for (Listener listener : listeners) {
                listener.call(1);
            }
        }
    }

    public static void updateLogPushedDeviceInfo(List<String> pushedDevice, String messageUuid) {


    }

}
